home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12733 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  58 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Mikael Andersson <prmiand@stog.wmdata.se>
  4. Subject: Re: file processing...
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <31514C27.58F3@stog.wmdata.se>
  7. Date: Thu, 21 Mar 1996 12:31:35 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <4iqr98$i3p@netnews.upenn.edu>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0GoldB2 (Win95; I)
  13. Organization: WM Data Infrateknik
  14.  
  15. Sonny wrote:
  16. > I want to read the 1st line of a text file into an array. The only way I know of to
  17. > detect the end of a line is by checking for "\n", but the code I wrote does not seem
  18. > be detecting it. Here is the code...
  19. > #include<fstream.h>
  20. > #include<iostream.h>
  21. > #include<String.h>
  22. > int readf(char array[],char *);
  23. > int readf (char array[], char *filename)
  24. > {
  25. >   int i=0;
  26. >   char c;
  27. >   ifstream inFile(filename, ios::in);
  28. >   while (inFile >> c)
  29. >     if (c!="\n")      /*This line is giving me problem. It doesn't even compile */
  30. >       array[i++]=c;
  31. >     else
  32. >       break;
  33. >   return i;
  34. > }
  35. > void main(int argc,char *argv[])
  36. > {
  37. >   int i;
  38. >   char array[50];
  39. >   i=readf(array,"gro");
  40. >   for(int c=0;c<i;c++)
  41. >     cout<<array[c];
  42. >   cout<<"\n";
  43. > }
  44. > --Either you can use inFile.getline(buffer_pointer,max_length,'\n') (maybe 
  45. readline I don't remember), or you can change to !='\n' or !=0x0A. I've 
  46. never used the >> so I can be wrong but there's a inFile.read(...,1) 
  47. which would read one character at a time so you could use that instead.
  48.  
  49. Hope it helps, Micke.
  50.